fromjsdevspace.substack.com
6 months agoJavaScript Just Leveled Up: ES2025 - You'll Fall in Love With
Tired of endless conditionals? Pattern matching transforms branching logic into declarative expressions. Old approach: function handleResponse(response) { if (response.status === 200 && response.data) { return response.data; } else if (response.status === 401) { throw new Error('Unauthorized'); } else if (response.status === 404) { throw new Error('Not Found'); } else if (response.status >= 500) { throw new Error('Server Error'); } else { throw new Error('Unknown Error'); } } New ES2025 magic: function handleResponse(response) { return match (response) { when ({ status: 200, data }) -> data when ({ status: 401 }) -> throw new Error('Unauthorized') when ({ status: 404 }) -> throw new Error('Not Found') when ({ status: s if s >= 500 }) -> throw new Error('Server Error') default -> throw new Error('Unknown Error') }; } It's concise, readable, and surprisingly powerful like switch statements on steroids.